package cn.shiwei.pet.controller;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.util.LoginContextUtil;
import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.pet.domain.Pet;
import cn.shiwei.pet.query.PetQuery;
import cn.shiwei.pet.service.IPetService;
import cn.shiwei.user.domain.Logininfo;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    public PageList<Pet> getPget(@RequestBody PetQuery query){
        return petService.queryDate(query);
    }

    @PostMapping("/onsale")
    public RequestStatus onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PostMapping("/offsale")
    public RequestStatus offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PostMapping("/list")
    public RequestStatus getPetList(@RequestBody PetQuery query){
        try {
            query.setState(Pet.ON_SALE_STATUS);
            PageList<Pet> pageList = petService.queryDate(query);
            return RequestStatus.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }

    }

    @GetMapping("/getPet/{id}")
    public RequestStatus getPet(@PathVariable("id") Long id){
        try {
            Pet pet = petService.loadPetAndDetailByIDState(id, Pet.ON_SALE_STATUS);
            return RequestStatus.me().setResultObj(pet);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }
}
